package com.system.jfm.MainTables;
import com.mysql.cj.x.protobuf.MysqlxCrud;
import com.system.Daos.StudentDao;
import com.system.Models.Student;
import com.system.jfm.Components.*;
import com.system.jfm.MainTable;
import com.system.tools.MyFunction;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;

public class StudentInfo extends JPanel{

    TopPanel topPanel;
    // 表格
    JTable table;
    JScrollPane scrollPane;
    // 右键菜单
    RightMenu rightMenu;

    MyFunction updateTableFunc;

    private void updateTableModel(DefaultTableModel model) {



        table.setModel(model);
        //设置列宽
        TableColumnModel columnModel = table.getColumnModel();
        TableColumn column1 = columnModel.getColumn(0);
        TableColumn column2 = columnModel.getColumn(1);
        TableColumn column3 = columnModel.getColumn(2);
        column1.setPreferredWidth(150);
        column2.setPreferredWidth(150);
        column3.setPreferredWidth(150);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setFillsViewportHeight(true);


    }

    public void initTable() throws SQLException {
        DefaultTableModel model = new StudentDao().getAllTableModel();
        updateTableModel(model);
    }

    public StudentInfo(MyFunction updateTableFunc) throws SQLException {
        this.updateTableFunc = updateTableFunc;
        this.setBackground(Color.white);
        this.setLayout(new BorderLayout());

        rightMenu = new RightMenu();
        topPanel = new TopPanel();
        table = new JTable();

        this.initTable();

        scrollPane = new JScrollPane(table);

        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                boolean flag = e.isPopupTrigger();
                if (flag) {
                    rightMenu.show(table, e.getX(), e.getY());
                }
            }
        });

        rightMenu.addItem.addActionListener(addStudentListener);
        topPanel.add_btn.addActionListener(addStudentListener);

        rightMenu.alterItem.addActionListener(alterStudentListener);
        topPanel.alter_btn.addActionListener(alterStudentListener);

        rightMenu.deleteItem.addActionListener(deleteStudentListener);
        topPanel.delete_btn.addActionListener(deleteStudentListener);

        rightMenu.selectItem.addActionListener(searchStudentListener);
        topPanel.select_btn.addActionListener(searchStudentListener);

        topPanel.show_all.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    initTable();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        this.add(rightMenu);
        this.add(scrollPane, BorderLayout.CENTER);
        this.add(topPanel, BorderLayout.NORTH);
    }

    ActionListener addStudentListener = e -> {
        String[] genders = {"男", "女"};
        JTextField idField = new JTextField();
        JTextField nameField = new JTextField();
        JComboBox<String> sexComboBox = new JComboBox<String>(genders);

        Object[] message = {
                "学号", idField,
                "姓名", nameField,
                "性别", sexComboBox,
        };
        String id = "";
        String name = "";
        String sex = "男";
        while (true) {
            idField.setText(id);
            nameField.setText(name);
            sexComboBox.setSelectedItem(sex);
            int option = JOptionPane.showConfirmDialog(null, message, "请输入学号，姓名和性别", JOptionPane.OK_CANCEL_OPTION);
            if (option == JOptionPane.CANCEL_OPTION || option == JOptionPane.CLOSED_OPTION) {
                break;
            }
            id = idField.getText();
            name = nameField.getText();
            sex = sexComboBox.getSelectedItem().toString();
            if ((!idField.getText().equals("")) && (!nameField.getText().equals(""))) {
                if (option == JOptionPane.OK_OPTION) {
                    boolean flag;
                    try {
                        flag = new StudentDao(new Student(id, name, sex)).addStudent();
                        this.updateTableFunc.exec();
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }
                    if (flag) {
                        break;
                    } else {
                        new Message("添加失败");
                    }
                }
            } else {
                new Message("数据不能为空");
            }
        }
    };

    ActionListener alterStudentListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            int selectRow = table.getSelectedRow();
            if (selectRow==-1){
                new Message("请选中后修改");
                return;
            }
            String[] genders = {"男", "女"};
            JTextField idField = new JTextField();
            JTextField nameField = new JTextField();
            JComboBox<String> sexComboBox = new JComboBox<String>(genders);
            Object[] message = {
                    "学号", idField,
                    "姓名", nameField,
                    "性别", sexComboBox,
            };
            String id = table.getValueAt(selectRow, 0).toString();
            String name = table.getValueAt(selectRow, 1).toString();
            String sex = table.getValueAt(selectRow, 2).toString();
            idField.setText(id);
            nameField.setText(name);
            sexComboBox.setSelectedItem(sex);
            idField.setEnabled(false);
            while (true) {
                int option = JOptionPane.showConfirmDialog(null, message, "修改", JOptionPane.OK_CANCEL_OPTION);
                if (option == JOptionPane.CANCEL_OPTION || option == JOptionPane.CLOSED_OPTION) {
                    break;
                }
                name = nameField.getText();
                sex = sexComboBox.getSelectedItem().toString();
                if (((!nameField.getText().equals("")))){
                    if (option == JOptionPane.OK_OPTION) {
                        boolean flag;
                        try {
                            flag = new StudentDao(new Student(id, name, sex)).alterStudent();
                            initTable();
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        }
                        if (flag) {
                            break;
                        } else {
                            new Message("修改失败");
                        }
                    }
                } else {
                    new Message("数据不能为空");
                }
            }
        }
    };
    ActionListener deleteStudentListener=new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            int selectRow = table.getSelectedRow();
            JTextField idField = new JTextField();
            Object[] message = {
                    "学号", idField,
            };
            if (selectRow<0){
               new Message("必须选中表中要删除的内容");
               return;
            }
            String id = table.getValueAt(selectRow, 0).toString();
            idField.setText(id);
            while (true) {
                int option = JOptionPane.showConfirmDialog(null, message, "删除", JOptionPane.OK_CANCEL_OPTION);
                if (option == JOptionPane.CANCEL_OPTION || option == JOptionPane.CLOSED_OPTION) {
                    break;
                }
                id = idField.getText();
                if ((!idField.getText().equals(""))) {
                    if (option == JOptionPane.OK_OPTION) {
                        boolean flag;
                        try {
                            flag = new StudentDao(new Student(id)).deleteStudent();
                            initTable();
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        }
                        if (flag) {
                            break;
                        } else {
                            new Message("删除失败");
                        }
                    }else {
                        new Message("数据不能为空");
                    }
                }
            }
        }
    };
   ActionListener searchStudentListener=new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e) {
           String[] genders = {"男", "女"};
           JTextField idField = new JTextField();
           JTextField nameField = new JTextField();
           JComboBox<String> sexComboBox = new JComboBox<String>(genders);
           Object[] message = {
                   "学号", idField,
                   "姓名", nameField,
                   "性别", sexComboBox,
                    };
           int option = JOptionPane.showConfirmDialog(null, message, "查找", JOptionPane.OK_CANCEL_OPTION);
           String id = idField.getText();
           String name = nameField.getText();
           String sex = sexComboBox.getSelectedItem().toString();
            if (option == JOptionPane.OK_OPTION) {
                try {
                    DefaultTableModel model = new StudentDao(new Student(id, name, sex)).searchStudent();
                    updateTableModel(model);
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
            }
        };
}
